package com.cloudm.technician.ui.modules.cloudm_box.machine_list

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.View
import android.view.ViewGroup
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.logger_e
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseAdapter
import com.cloudm.technician.ui.base.BaseListFragment
import com.cloudm.technician.ui.modules.cloudm_box.device_detail.DeviceDetailActivity
import com.cloudm.technician.ui.widget.OnceClickListener
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem
import kotlinx.android.synthetic.main.common_empty_view.*
import kotlinx.android.synthetic.main.fragment_machine_list.*

/**
 * 作者: wesley
 * 时间: 2017/12/18
 * 描述: 机器列表ui
 */
class MachineListFragment : BaseListFragment<MachineListPresenter, DeviceInfo>(),
        MachineListContract.View, MachineListDialogFragment.OnDialogItemClickListener,
        MachineListAdapter.OnItemClickListener {

    override val enabledRefresh: Boolean
        get() = true

    private lateinit var adapter: MachineListAdapter

    override val resource: Int
        get() = R.layout.fragment_machine_list

    companion object {
        private const val TAG = "MachineListFragment"
        fun newInstance() = MachineListFragment()
    }

    override fun onItemClick(t: DeviceInfo) {
        presenter?.openActivityByStatus(1, t)
    }

    override fun onMachineStatusClick(info: DeviceInfo) {
        openActivityByStatus(info)
    }

    override fun initInject() {
        DaggerMachineListComponent.builder().appComponent(appComponent).machineListPresenterModule(
                MachineListPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {

    }

    override fun createAdapter(): BaseAdapter<DeviceInfo> {
        adapter = MachineListAdapter()
        adapter.listener = this
        return adapter
    }

    override fun onResume() {
        super.onResume()
        refresh()
    }

    override fun loadData() {
        presenter?.getMachineListInfo(startPage, PAGE_SIZE)
    }


    override fun initSwipeMenu() {
        recyclerView.setSwipeMenuCreator { _, swipeRightMenu, _ ->
            val menuItem = SwipeMenuItem(mContext).apply {
                setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_ff0000))
                setText(R.string.delete)
                setTextColor(ContextCompat.getColor(mContext, R.color.common_color_ffffff))
                textSize = 16
                height = ViewGroup.LayoutParams.MATCH_PARENT
                width = mContext.resources.getDimensionPixelSize(R.dimen.item_machine_menu_height)
            }
            swipeRightMenu?.addMenuItem(menuItem)
        }

        recyclerView.setSwipeMenuItemClickListener { menuBridge ->
            menuBridge?.closeMenu()
            val adapterPosition = menuBridge?.adapterPosition ?: -1
            if (adapter.list.size > adapterPosition && adapterPosition >= 0) {
                showDeleteDialog(adapter.list[adapterPosition])
            } else {
                mContext.toast(mContext.resources.getString(R.string.unknow_error))
            }
        }
    }

    override fun deleteSuccess() {
        mContext.toast(mContext.resources.getString(R.string.device_delete_success))
        refresh()
    }

    override fun deleteFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    private fun showDeleteDialog(info: DeviceInfo) {
        val fragment = MachineListDialogFragment.newInstance(info)
        fragment.listener = this
        fragment.show(childFragmentManager, TAG)
    }

    override fun onDeleteClick(info: DeviceInfo) {
        presenter?.deleteDevice(info.id ?: -1)
    }

    override fun initListener() {
        super.initListener()
        btnAdd.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                openDeviceDetailActivity(0, null)
            }
        })
    }

    override fun showEmptyView() {
        super.showEmptyView()
        btnAdd.visibility = View.GONE

        emptyValue.text = mContext.resources.getString(R.string.add_device)
        emptyValue.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                openDeviceDetailActivity(0, null)
            }
        })
    }

    override fun showContentView() {
        super.showContentView()
        btnAdd.visibility = View.VISIBLE
    }


    override fun openDeviceDetailActivity(flag: Int, info: DeviceInfo?) {
        val intent = Intent(mContext, DeviceDetailActivity::class.java).apply {
            putExtras(Bundle().apply {
                putInt("flag", flag)
                putParcelable("info", info)
            })
        }
        mContext.openActivity(intent, false)
    }

    override fun showDoneStatusMsg() {
        mContext.logger_e(TAG, " status is 8 or status is 9")
    }
}